package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import java.util.*; 

/**
 * User interface class to the AddressBook
 * 
 * @author (Group1:Georgi,Roland and Mindaugas) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    private Scanner keyboard;
    
    /**
     * constructor of AddressBookUI.
     */
    public AddressBookUI()
    {
       addrCtr=new AddressCtr();
       keyboard = new Scanner(System.in);
    }
    
    /**
     * Starts the program.
     */
    public void start()
    {
        printWelcomeMessage();
        addressMenu();
      
    }
    /**
     * Adress menu.
     */
    private void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
         
         int choise = writeAddressBookMenu();
          if(choise==1)
          {   //show person
              getPerson();
          
          }
          else{
              if(choise==2){
                    createPerson();
              
              }  
              else{
                  if(choise==3){
                      deletePerson();
                   
                  }  
                  else{
                      if(choise==4){
                          updatePerson();
                 
                      }
                      else{
                          if(choise==5){
                              listAllPersons();
    
                            }
                          else{
                              if (choise==6)
                              exit = true;
                              
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }
    }
     /**
      * Prints the Addressbook menu.
      */
    private void printWelcomeMessage()
    {
    
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
    }
    
    /**
     * writes the AddressBookMenu.
     */
    private int writeAddressBookMenu()
    {    
            
            
       while(true)
            {
                try{
 
                Scanner in= new Scanner(System.in);
                int choise = in.nextInt();
                return choise;
               }
             
               catch(InputMismatchException e)
               {
                 System.out.println("Please use right input and try again.");
               }

            } 
            
           
            
        }
        
   /**
    * Allows the uset to set the persons id.
    */
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
      
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please use right input and try again.");
             
           }
        }//end while
        return id;
    }
    
    /**
     * Allows the uset to set the name
     */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen

         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
    
   /**
    * Allows the uset to set the postal code.
    */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         
         Scanner keyboard = new Scanner(System.in);  
          System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * Allows the uset to set the city.
     */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
   
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    
    /**
     * Allows the uset to set the phone.
     */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
        
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    
    /**
     * Allows the uset to set the Address.
     */
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
   
    /**
     * Searches for the Person.
     */
    private void getPerson()
    {
        printWelcomeMessage();
        System.out.println();
        long id = inputPersonsID();
              
        if(!addrCtr.personExist(id))
        {
           System.out.println("Person doesn't exist,please try again");
           return;
        }
        
        try{

        System.out.println("Name: "+addrCtr.getPersonName(id));
        System.out.println("Address: "+addrCtr.getPersonAddress(id));
        System.out.println("Postal code: "+addrCtr.getPersonPostalCode(id));
        System.out.println("City: "+addrCtr.getPersonCity(id));
        System.out.println("Phone: "+addrCtr.getPersonPhone(id));
    }
    catch(Exception e){
        System.out.println("Person Does not exist, please try again");
    }
    
    }   
    
    /**
     * Creates a person.
     */
    private void createPerson()
    {
      
        long id=inputPersonsID();
        if(addrCtr.personExist(id))
        {
           System.out.println("Person ID already exist,please try again");
           return;
        }
         
       try{
           System.out.println();
           addrCtr.createPerson(id, inputName(), inputAddress(),inputPostalCode(), inputCity(), inputPhone() );
           printWelcomeMessage();
        }  
        
       catch(Exception e){
       System.out.println("Error occured while creating the Person, please try again.");
        
        }
        
    }
    
    /**
     * creates a person.
     */
    private void deletePerson()
    {
       long id=inputPersonsID();
       
        if(!addrCtr.personExist(id))
        {
           System.out.println("Person doesn't exist,please try again");
           return;
        }
        
        try{
           addrCtr.deletePerson(id);
           printWelcomeMessage();
        }  
        
       catch(Exception e){
       System.out.println("Error occured while deleting the person, please try again.");
        
        }
      
    }
    
    /**
     * updates a person
     */
    private void updatePerson()
    {
        long id=inputPersonsID();
        
        if(!addrCtr.personExist(id))
        {
           System.out.println("Person doesn't exist,please try again");
           return;
        }
        
        try{
            id=inputPersonsID();
            addrCtr.getPersonName(id);
            addrCtr.updatePerson(id, inputName(), inputPostalCode(), inputCity(), inputPhone(), inputAddress());
             printWelcomeMessage();
        }  
        
       catch(Exception e){
       System.out.println("Error occured while updating the Person, please try again.");
        
        }
       
    }
    
    /**
     * Lists all persons
     */
    public void listAllPersons()
    {
    
        try{
        ArrayList<Long> IDs = addrCtr.getPersonsIDs();
        ArrayList<String> names = addrCtr.getPersonsNames();
        ArrayList<String> addresses = addrCtr.getPersonsAddress();
        ArrayList<String> postalCodes = addrCtr.getPersonsPostalCodes();
        ArrayList<String> cities = addrCtr.getPersonsCities();
        ArrayList<String> phones = addrCtr.getPersonsPhones();    
        if(IDs.isEmpty()){
                System.out.println("The list of persons is empty.");
        
           }else  for(int index=0; index<IDs.size(); index++){
               System.out.println((index+1)+".ID: "+IDs.get(index)+"  Name: " + names.get(index) +"  Address: "+ addresses.get(index)+"  Postal code: " + postalCodes.get(index) +"  City: " + cities.get(index)+"  Phone: " + phones.get(index));
            }
       }  
        
       catch(Exception e){
       System.out.println("Error occured while listing all persons, please try again.");
        
       }
     
    }
}
            
        
